package com.clock.service.impl;

import com.clock.common.constant.DiscussPostCacheKey;
import com.clock.common.constant.HttpStatus;
import com.clock.common.domain.R;
import com.clock.common.exception.BizException;
import com.clock.common.utils.RedisUtil;
import com.clock.domain.dto.PostLikeDTO;
import com.clock.domain.dto.PostsLikeDTO;
import com.clock.mapper.PostRelatedTagMapper;
import com.clock.service.ContentUserLikeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;


@Service
public class ContentUserLikeServiceImpl implements ContentUserLikeService {

    @Resource
    RedisUtil redisUtil;

    @Resource
    PostRelatedTagMapper postRelatedTagMapper;

    /**
     * 点赞/取消赞
     *
     * @param postLikeDTO
     * @return
     */
    @Override
    public R updateDiscussPostLikeState(PostLikeDTO postLikeDTO) {

        if (postLikeDTO.getUserId() == null || postLikeDTO.getPostId() == null || postLikeDTO.getIsLiked() == null || postLikeDTO.getAction() == null) {
            throw new BizException(HttpStatus.BAD_REQUEST, "参数异常");
        }

        String POST_LIKE_CACHE_KEY = DiscussPostCacheKey.DISCUSS_POST_LIKE_CACHE_KEY + postLikeDTO.getPostId();

        String USER_LIKE_CONTENT_CACHE_KEY = DiscussPostCacheKey.USER_LIKE_CACHE_KEY + postLikeDTO.getUserId();

        System.out.println(POST_LIKE_CACHE_KEY);
        System.out.println(USER_LIKE_CONTENT_CACHE_KEY);

        // 判断该集合是否存在元素
        boolean isExist = redisUtil.sHasKey(POST_LIKE_CACHE_KEY, postLikeDTO.getUserId());

        // 点赞状态true 说明点赞过
        if (postLikeDTO.getIsLiked() == true){
            if (postLikeDTO.getAction() == 1) {
                throw new BizException(HttpStatus.BAD_REQUEST, "重复点赞");
            } else {
                if (isExist) {
                    redisUtil.setRemove(POST_LIKE_CACHE_KEY, postLikeDTO.getUserId());
                    redisUtil.setRemove(USER_LIKE_CONTENT_CACHE_KEY, postLikeDTO.getPostId());
                    return R.ok("取消点赞成功");
                }
                // TODO 不存在缓存需不要操作
            }

        // 点赞状态为false
        }else{
            // 执行点赞
            if (postLikeDTO.getAction() == 1) {
                if (isExist) {
                    throw new BizException(HttpStatus.BAD_REQUEST, "重复点赞");
                }
                redisUtil.sSetAndTime(POST_LIKE_CACHE_KEY, TimeUnit.DAYS.toSeconds(30), postLikeDTO.getUserId());
                redisUtil.sSetAndTime(USER_LIKE_CONTENT_CACHE_KEY, TimeUnit.DAYS.toSeconds(30), postLikeDTO.getPostId());
                return R.ok("点赞成功");
            } else {
                throw new BizException(HttpStatus.BAD_REQUEST, "重复取消点赞");
            }
        }
        return R.fail("操作失败");
    }


    /**
     * 查询该帖子的点赞数以及判断是否已点赞
     *
     * @param postsLikeDTO
     * @return
     */
    @Override
    public R queryDiscussPostLikeCountAndIsLike(PostsLikeDTO postsLikeDTO) {

        Long[] postIds = postsLikeDTO.getPostIds();

        Long userId = postsLikeDTO.getUserId();


        if ( postIds== null || postsLikeDTO.getUserId() == null) {
            System.out.println("!!!!"+postIds);
            System.out.println(postsLikeDTO.getUserId());
            //throw new BizException(HttpStatus.BAD_REQUEST, "参数异常");
        }
        List<Map<String, Object>> list = new LinkedList<>();

        for (Long postId : postIds) {
            Map<String, Object> map = new HashMap<>();
            String POST_LIKE_CACHE_KEY = DiscussPostCacheKey.DISCUSS_POST_LIKE_CACHE_KEY + postId;
            // 判断该集合是否存在元素
            boolean isLiked = redisUtil.sHasKey(POST_LIKE_CACHE_KEY, userId);

            //查询该集合的元素个数
            long likeCount = redisUtil.sGetSetSize(POST_LIKE_CACHE_KEY);

            map.put("postId", postId);
            map.put("isLiked", isLiked);
            map.put("likeCount", likeCount);

            list.add(map);
        }


        return R.ok(list);
    }

    /**
     * 查询用户最喜欢的帖子标签
     * @param userId
     * @return
     */
    @Override
    public String queryUserBestLikeTag(Long userId) {
        String USER_LIKE_CONTENT_CACHE_KEY = DiscussPostCacheKey.USER_LIKE_CACHE_KEY + userId;
        Set<Object> postIds = redisUtil.sGet(USER_LIKE_CONTENT_CACHE_KEY);

        if (postIds == null || postIds.isEmpty()) {
            return "";
        }

        String BestLikeTag = postRelatedTagMapper.queryMostLikedTag(postIds);

        return BestLikeTag;
    }

}
